package com.xbdun.service.bmp.facade;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.exceptions.BmpBizException;
import com.xbdun.facade.bmp.request.system.ModifyDepartRoleRequest;
import com.xbdun.facade.bmp.result.system.OrleDepartMenusResult;
import com.xbdun.facade.bmp.service.system.PowerRoleDepartFacade;
import com.xbdun.service.bmp.service.system.PowerRoleDepartService;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class PowerRoleDepartFacadeImpl implements PowerRoleDepartFacade{
	
	@Autowired
	PowerRoleDepartService powerRoleDepartService;
	public XbdUnStringResult modifyDepartRole(ModifyDepartRoleRequest request, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 部门关联菜单设置权限,{}", request);
			CommonUtil.validateClient(client);
			if(null==request.getOrleDepartId()||request.getOrleDepartId().longValue()<=0){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("部门角色编号不合法");
			}
			//如果删除与新增都不存在，那么说明这次的操作就是无效的
			if(CollectionUtils.isEmpty(request.getAddMenuCode())&&CollectionUtils.isEmpty(request.getDelMenuCode())){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("更改的菜单项不能为空,请选择一项");
			}
			powerRoleDepartService.mofidyDepartRole(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("部门关联菜单设置权限,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 部门关联菜单设置权限结果:{}", result);
		}
		return result;
	}
	public OrleDepartMenusResult queryOrleDepartMenus(Long orleDepartId, Client client) {
		OrleDepartMenusResult result = new OrleDepartMenusResult();
		try{
			log.info("start - 部门关联菜单设置权限,{}", orleDepartId);
			CommonUtil.validateClient(client);
			if(null==orleDepartId||orleDepartId.longValue()<=0){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("部门角色编号不合法");
			}
			//获取到当前机构下的部门对应角色所有权限
			result.setData(powerRoleDepartService.queryOrleDepartMenus(orleDepartId,client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("部门关联菜单设置权限,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 部门关联菜单设置权限结果:{}", result);
		}
		return result;
	}

}
